/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.elicitor.core;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

/**
 *
 * @author dileepa
 */
public class TestAlgo {
    
    
    String realTimeFileLoc = "/home/dileepa/Desktop/kmeansTest/test/allFiles";
    int fileNo=0;
    int counter =1;
    
    Double angerData[] = new Double[]{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};
    Double disgustData[] = new Double[]{1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0};
    Double happyData[] = new Double[]{2.0,2.0,2.0,2.0,2.0,2.0,2.0,2.0,2.0,2.0,2.0,2.0,2.0,2.0,2.0,2.0,2.0};
    Double fearData[] = new Double[]{3.0,3.0,3.0,3.0,3.0,3.0,3.0,3.0,3.0,3.0,3.0,3.0,3.0,3.0,3.0,3.0,3.0};
    Double surpriseData[] = new Double[]{4.0,4.0,4.0,4.0,4.0,4.0,4.0,4.0,4.0,4.0,4.0,4.0,4.0,4.0,4.0,4.0,4.0};
    Double sadnessData[] = new Double[]{5.0,5.0,5.0,5.0,5.0,5.0,5.0,5.0,5.0,5.0,5.0,5.0,5.0,5.0,5.0,5.0,5.0};
    
    Double tempAngerData[] = new Double[17];
    Double tempDisgustData[] = new Double[17];
    Double tempHappyData[] = new Double[17];
    Double tempFearData[] = new Double[17];
    Double tempSadnessData[] = new Double[17];
    Double tempSurpriseData[] = new Double[17];
    
    Set<Integer> tempAngerFileNo = new HashSet<Integer>();
    Set<Integer> tempDisgustFileNo = new HashSet<Integer>();
    Set<Integer> tempFearFileNo = new HashSet<Integer>();
    Set<Integer> tempHappyFileNo = new HashSet<Integer>();
    Set<Integer> tempSadnessFileNo = new HashSet<Integer>();
    Set<Integer> tempSurpriseFileNo = new HashSet<Integer>();
    
    
    double deviateFromAnger;
    double deviateFromDisgust;
    double deviateFromHappy;
    double deviateFromFear;
    double deviateFromSurprise;
    double deviateFromSadness;
    
    Map <Integer, ArrayList<Double>> resultAngerMap = new HashMap <Integer, ArrayList<Double>>();
    Map <Integer, ArrayList<Double>> resultDisgustMap = new HashMap <Integer, ArrayList<Double>>();
    Map <Integer, ArrayList<Double>> resultHappyMap = new HashMap <Integer, ArrayList<Double>>();
    Map <Integer, ArrayList<Double>> resultFearMap = new HashMap <Integer, ArrayList<Double>>();
    Map <Integer, ArrayList<Double>> resultSurpriseMap = new HashMap <Integer, ArrayList<Double>>();
    Map <Integer, ArrayList<Double>> resultSadnessMap = new HashMap <Integer, ArrayList<Double>>();
    
    List <Double> resultAngerList = new ArrayList<Double>();
    List <Double> resultDisgustList = new ArrayList<Double>();
    List <Double> resultHappyList = new ArrayList<Double>();
    List <Double> resultFearList = new ArrayList<Double>();
    List <Double> resultSurpriseList = new ArrayList<Double>();
    List <Double> resultSadnessList = new ArrayList<Double>();
    
   
    
    
    public static void main(String args[]){
        TestAlgo test = new TestAlgo();
        test.detectEmotion();
    }
    
    
    public String detectEmotion(){
        
        counter++;
        System.out.println("recursion time "+counter);
        Scanner scanner = null;
        File file = new File(realTimeFileLoc);
        List <Double>realtimeDataList = null;
        String suitableEmotion = null;
//        removeAllItemMap(resultAngerMap);
//        removeAllItemMap(resultDisgustMap);
//        removeAllItemMap(resultFearMap);
//        removeAllItemMap(resultHappyMap);
//        removeAllItemMap(resultSadnessMap);
//        removeAllItemMap(resultSurpriseMap);
        resultFearMap = new HashMap <Integer, ArrayList<Double>>();
        resultAngerMap = new HashMap <Integer, ArrayList<Double>>();
        resultDisgustMap = new HashMap <Integer, ArrayList<Double>>();
        resultSadnessMap = new HashMap <Integer, ArrayList<Double>>();
        resultSurpriseMap = new HashMap <Integer, ArrayList<Double>>();
        resultHappyMap = new HashMap <Integer, ArrayList<Double>>();
        
        fileNo=0;
        
        try {
 
             scanner = new Scanner(file);
 
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                String result[] = line.split(",");
                System.out.println(line);
                System.out.println(result[0]);
        if(result.length == 17){ 
            
            fileNo++;
            realtimeDataList = null;
            realtimeDataList = new ArrayList<Double>();
            for (String s : result){
                        realtimeDataList.add(Double.parseDouble(s));

            }
           deviateFromAnger = calcuDistance(realtimeDataList, angerData);
           deviateFromDisgust = calcuDistance(realtimeDataList, disgustData);
           deviateFromFear = calcuDistance(realtimeDataList, fearData);
           deviateFromHappy = calcuDistance(realtimeDataList, happyData);
           deviateFromSadness = calcuDistance(realtimeDataList, sadnessData);
           deviateFromSurprise = calcuDistance(realtimeDataList, surpriseData);
           
           Double devation[] = new Double[]{deviateFromAnger,deviateFromDisgust,deviateFromFear,deviateFromHappy,deviateFromSadness,deviateFromSurprise};
            
           findminimum(devation,realtimeDataList);
     
        }
            }
            
      
         if (tempAngerFileNo==resultAngerMap.keySet() && tempDisgustFileNo == resultDisgustMap.keySet() && tempFearFileNo==resultFearMap.keySet()
                  && tempHappyFileNo==resultHappyMap.keySet() && tempSadnessFileNo==resultSadnessMap.keySet() && tempSurpriseFileNo==resultSurpriseMap.keySet()){
              
              System.out.println("anger Map "+resultAngerMap.keySet());
              System.out.println("disgust Map "+resultDisgustMap.keySet());
              System.out.println("Happy Map "+resultHappyMap.keySet());
              System.out.println("fear Map "+resultFearMap.keySet());
              System.out.println("sadness map "+resultSadnessMap.keySet());
              System.out.println("surprise map "+resultSurpriseMap.keySet());
              
          }
          else{
             
            if (!resultAngerMap.isEmpty()){
              tempAngerFileNo=resultAngerMap.keySet();
              angerData=findNewCentroid(resultAngerMap);
                System.out.println("change anger centroid ********************************************************************************");
          }
          if (!resultDisgustMap.isEmpty()){
              tempDisgustFileNo = resultDisgustMap.keySet();
              disgustData=findNewCentroid(resultDisgustMap);
              System.out.println("change disgust centroid********************************************************************************");
          }
          if (!resultFearMap.isEmpty()){
              tempFearFileNo = resultFearMap.keySet();
              fearData=findNewCentroid(resultFearMap);
              System.out.println("change Fear centroid********************************************************************************");
          }
          if (!resultHappyMap.isEmpty()){
              tempHappyFileNo=resultHappyMap.keySet();
              happyData=findNewCentroid(resultHappyMap);
              System.out.println("change happy centroid********************************************************************************");
          }
          if (!resultSadnessMap.isEmpty()){
              tempSadnessFileNo=resultSadnessMap.keySet();
              sadnessData=findNewCentroid(resultSadnessMap);
              System.out.println("change Sadness centroid********************************************************************************");
          }
          if (!resultSurpriseMap.isEmpty()){
              tempSurpriseFileNo=resultSurpriseMap.keySet();
              surpriseData=findNewCentroid(resultSurpriseMap);
              System.out.println("change Surprise centroid********************************************************************************");
          } 
             
             
          detectEmotion();
          }
            
            
            
            
         
          
          
          
          
          
          
          
          
          
         
          
          
            
        }
            
            catch (Exception e){
                System.err.println(e.getMessage());
            }
        finally{
            scanner.close();
        }
        
        return suitableEmotion;
       
    }

    private Double calcuDistance(List<Double> realtimeDataList, Double emotion[]) {
        
        double d=0;
        
        for (int i=0; i<17;i++){
            
           d = d + (realtimeDataList.get(i)-emotion[i])*(realtimeDataList.get(i)-emotion[i]); 
        }
        
        return d;
    }

    private void findminimum(Double[] devation, List<Double> realtimeDataList) {
        int j = 0;
        
        
        
        for (int i=0;i<devation.length;i++){
            System.out.println("devation....."+devation[i]);
            if(devation[i]<devation[j]){
                j=i;
            }
        }
        
        if (j==0){
            resultAngerMap.put(fileNo, (ArrayList<Double>)realtimeDataList);
        }else if(j==1){
            resultDisgustMap.put(fileNo, (ArrayList<Double>)realtimeDataList);
        }else if(j==2){
            resultFearMap.put(fileNo, (ArrayList<Double>)realtimeDataList);
        }else if(j==3){
            resultHappyMap.put(fileNo, (ArrayList<Double>)realtimeDataList);
        }else if(j==4){
            resultSadnessMap.put(fileNo, (ArrayList<Double>)realtimeDataList);
        }else if(j==5){
            resultSurpriseMap.put(fileNo, (ArrayList<Double>)realtimeDataList);
            
        }
        
        

    }

    private Double[] findNewCentroid(Map<Integer, ArrayList<Double>> resultMap) {
        
        int i=0;
        double att1=0.0,att2=0.0,att3=0.0,att4=0.0,att5=0.0,att6=0.0,att7=0.0,att8=0.0,att9=0.0,att10=0.0,att11=0.0,att12=0.0,att13=0.0,att14=0.0,att15=0.0,att16=0.0,att17=0.0;
        
        for(ArrayList<Double> list : resultMap.values()){
            
             att1 =att1+list.get(0);
             att2 =att2+list.get(1);
             att3 =att3+list.get(2);
             att4 =att4+list.get(3);
             att5 =att5+list.get(4);
             att6 =att6+list.get(5);
             att7 =att7+list.get(6);
             att8 =att8+list.get(7);
             att9 =att9+list.get(8);
             att10 =att10+list.get(9);
             att11 =att11+list.get(10);
             att12 =att12+list.get(11);
             att13 =att13+list.get(12);
             att14 =att14+list.get(13);
             att15 =att15+list.get(14);
             att16 =att16+list.get(15);
             att17 =att16+list.get(16);
                       
        }
        
    
        Double Data[] = new Double[]{att1/resultMap.size(),att2/resultMap.size(),att3/resultMap.size(),att4/resultMap.size(),att5/resultMap.size(),att6/resultMap.size(),att7/resultMap.size(),
        att8/resultMap.size(),att9/resultMap.size(),att10/resultMap.size(),att11/resultMap.size(),att12/resultMap.size(),att13/resultMap.size(),
        att14/resultMap.size(),att15/resultMap.size(),att16/resultMap.size(),att17/resultMap.size()};
             
        return Data;
        
    }

    public void removeAllItemMap(Map <Integer, ArrayList<Double>> Map){
        
        for (int i : Map.keySet()){
            Map.remove(i);
        }
        
    }
    
}
